package com.zc.angelica.controller;

import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.IOException;

@RestController
public class LoginController {

    /**
     * 得到登陆验证码
     *
     * @param response
     * @param request
     * @throws
     */
    @RequestMapping("/getCode")
    public void getCode(HttpServletResponse response, HttpServletRequest request) throws IOException, FontFormatException {
        //HuTool定义图形验证码的长和宽,验证码的位数，干扰线的条数
        SpecCaptcha  captcha = new SpecCaptcha(130, 48);
        captcha.setLen(4);
        captcha.setFont(Captcha.FONT_1);
        //将验证码放入session
        request.getSession().setAttribute("validateCode", captcha.text());
        try {
            response.setContentType("image/gif");
            ServletOutputStream outputStream = response.getOutputStream();
            captcha.out(outputStream);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
